<?php
namespace waf\client\monitor;

use \waf\Waf;
use \waf\log\WLog;
use \waf\modules\WCFunc;
use \waf\client\rpc\WRpcClient;

class WMonitorClient
{
    /**
     * [module=>[interface=>time_start, interface=>time_start ...], module=>[interface=>time_start ..], ... ]
     * @var array
     */
    protected static $timeMap = array();

    /**
     * 模块接口上报消耗时间记时
     * @param string $module
     * @param string $interface
     * @return void
     */
    public static function tick($module = '', $interface = '')
    {
        $server_name    = Waf::getConfig('server', 'name');
        WLog::logDebug("in. server_name = $server_name, module = $module, interface = $interface", __FILE__, __LINE__, __FUNCTION__);

        self::$timeMap[$server_name][$module][$interface] = WCFunc::getCurrentMsectime();
    }

    /**
     * 上报统计数据
     * @param string $module
     * @param string $interface
     * @param bool $success
     */
    public static function report($module, $interface, $success)
    {
        $server_name    = Waf::getConfig('server', 'name');
        WLog::logDebug("in. server_name = $server_name, module = $module, interface = $interface, success = $success. timeMap = ".WLog::toJson(self::$timeMap), __FILE__, __LINE__, __FUNCTION__);

        if(isset(self::$timeMap[$server_name][$module][$interface]) && self::$timeMap[$server_name][$module][$interface] > 0)
        {
            $time_start = self::$timeMap[$server_name][$module][$interface];
            self::$timeMap[$server_name][$module][$interface] = 0;  // 记时复位
        }else{
            $time_start = WCFunc::getCurrentMsectime();
        }

        $cost_time = WCFunc::getCurrentMsectime() - $time_start;

        $arrParams = array(
            'url'               =>  '/monitor/interfaceStatistics',
            'server_name'       =>  $server_name,
            'module_name'       =>  $module,
            'interface_name'    =>  $interface,
            'cost_time'         =>  $cost_time,
            'success'           =>  $success,
        );

        $conf   = Waf::getConfig('rpc_server', 'monitor_server');
        WLog::logDebug("conf = ".WLog::toJson($conf), __FILE__, __LINE__, __FUNCTION__);

        WRpcClient::doSendUdpData($conf, $arrParams);
    }

    /**
     * 上报累计数据
     * @param string $module
     * @param string $interface
     * @param bool $success
     */
    public static function coutReport($module, $interface, $count = 1)
    {
        $server_name    = Waf::getConfig('server', 'name');
        WLog::logDebug("in. server_name = $server_name, module = $module, interface = $interface, count = $count", __FILE__, __LINE__, __FUNCTION__);

        $arrParams = array(
            'url'               =>  '/monitor/countStatistics',
            'server_name'       =>  $server_name,
            'module_name'       =>  $module,
            'interface_name'    =>  $interface,
            'count'             =>  $count,
        );

        $conf   = Waf::getConfig('rpc_server', 'monitor_server');
        WLog::logDebug("conf = ".WLog::toJson($conf), __FILE__, __LINE__, __FUNCTION__);

        WRpcClient::doSendUdpData($conf, $arrParams);
    }
}
?>
